﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace visu
{
    public class OperateContext
    {
        public const string CookiePath = "/admin/";
        public const string InfoKey = "info";
        public const string PermissionKey = "permission";
        public const string MenuPermissionKey = "MenuPermission";


        /// <summary>
        /// Http上下文
        /// </summary>
        static HttpContext ContextHttp
        {
            get
            {
                return HttpContext.Current;
            }
        }
        static System.Web.SessionState.HttpSessionState Session
        {
            get
            {
                return ContextHttp.Session;
            }
        }

        static HttpResponse Response
        {
            get
            {
                return ContextHttp.Response;
            }
        }

        static HttpRequest Request
        {
            get
            {
                return ContextHttp.Request;
            }
        }
        /// <summary>
        /// 用户基本信息
        /// </summary>
        public static UserInfo userInfo
        {
            get
            {
                if (Session[InfoKey] == null)
                {
                    GetSessionByCookie();
                }
                return Session[InfoKey] as UserInfo;
            }

        }


        public static List<Permission> SessPermission
        {
            get
            {
                if (Session[PermissionKey] == null)
                {
                    GetSessionByCookie();
                }
                return Session[PermissionKey] as List<Permission>;
            }

        }

        public static role SessMenuPermission
        {
            get
            {
                if (Session[MenuPermissionKey] == null)
                {
                    GetSessionByCookie();
                }
                return Session[MenuPermissionKey] as role;
            }

        }

        private static void GetSessionByCookie()
        {
            if (Request.Cookies[InfoKey] != null)
            {

                UserInfo authUsr = new UserInfo();
                string strUserInfo = Request.Cookies[InfoKey].Value;
                int userId = int.Parse(strUserInfo);
                user users = BLLSession.Current.userBLL.GetListBy(u => u.USER_ID == userId).FirstOrDefault();
             
                role role = BLLSession.Current.roleBLL.GetListBy(r => r.ROLE_NAME == users.ROLE&r.COMPANY_ID==users.COMPANY_ID).FirstOrDefault();
                if(role==null)
                {
                    Response.Write("<script>window.top.location.href='/Home/Login';</script>");
                    return;
                }
                string PermType = "";
                if (role.IS_Company == true)
                {
                    PermType += "IS_Company" + ",";
                }
                if (role.IS_Message == true)
                {
                    PermType += "IS_Message" + ",";
                }
                if (role.IS_Monitor == true)
                {
                    PermType += "IS_Monitor" + ",";
                }
                if (role.IS_Report == true)
                {
                    PermType += "IS_Report";
                }
                if (role.IS_Resource == true)
                {
                    PermType += "IS_Resource";
                }
                if (role.IS_Setting == true)
                {
                    PermType += "IS_Setting";
                } 
                List<Permission> permList = BLLSession.Current.PermissionBLL.GetListBy(p => PermType.Contains(p.PermType)).ToList();


                UserInfo userInfo = new UserInfo() { COMPANY_ID=users.COMPANY_ID, EMAIL = users.EMAIL, IS_ACTIVE = users.IS_ACTIVE, NAME = users.NAME, PASSWORD = users.PASSWORD, PHONE = users.PHONE, ROLE = users.ROLE, USER_ID = users.USER_ID, USERNAME = users.USERNAME };

                HttpContext.Current.Session[OperateContext.InfoKey] = userInfo;
                HttpContext.Current.Session[OperateContext.MenuPermissionKey] = role;
                HttpContext.Current.Session[OperateContext.PermissionKey] = permList;
            }
            else
            {
                //Response.Redirect("/Home/Login");

                Response.Write("<script>window.top.location.href='/Home/Login';</script>");

            }

        }
    }
}